<template>
  <div>
    <div class="login-contain">
      <van-nav-bar
        title="登录"
        class="page-nav-bar"
        left-arrow
        @click-left="$router.back()"
      />
    </div>
    <van-form @submit="onSubmit" :show-error="false" ref="form">
      <van-field
        v-model="user.mobile"
        type="number"
        name="mobile"
        label="手机号"
        clearable
        icon-prefix="heima"
        left-icon="heima-shouji"
        placeholder="请输入手机号"
        :rules="rules.mobile"
      />
      <van-field
        v-model="user.code"
        type="number"
        name="密码"
        label="验证码"
        clearable
        icon-prefix="heima"
        left-icon="heima-yanzhengma"
        placeholder="请输入验证码"
        :rules="rules.code"
      >
        <template #button>
          <van-button
            type="default"
            size="small"
            native-type="button"
            v-if="isCountDownShow"
            ><van-count-down
              :time="1000 * 3"
              @finish="isCountDownShow = false"
              format="ss秒"
          /></van-button>
          <van-button
            type="primary"
            size="small"
            native-type="button"
            v-else
            @click="sendSms"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { getSms, Login } from "../../api/user";
export default {
  name: "login",
  data() {
    return {
      isCountDownShow: false,

      user: {
        mobile: "18196767362",
        code: "246810",
      },
      rules: {
        mobile: [
          { required: true, message: "请填写密码" },
          { pattern: /^1[3|5|7|8]\d{9}$/ },
        ],
        code: [
          { required: true, message: "请填写密码" },
          { pattern: /^\d{6}$/, message: "验证码是六位数" },
        ],
      },
    };
  },
  methods: {
    async onSubmit() {
      const user = this.user;
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
      });
      let res;
      try {
        res = await Login(user);
      } catch (error) {
        console.log(error);
        this.$toast.fail("登录失败");
      }
      console.log(res);
      const { data } = res;
      this.$store.commit("setUser", data.data);
      this.$toast.success("登录成功");
    },
    async sendSms() {
      try {
        // await getSms(this.user.mobile);
        await this.$refs.form.validate("mobile");
        // return this.$toast.success("发送成功，请登录");
      } catch (error) {
        console.log(error);
        return this.$toast.fail("手机格式不正确");
      }
      try {
        await getSms(this.user.mobile);
        this.$toast.success("发送成功，请查收");
      } catch (error) {
        if (error.response && error.response.status === 429) {
          return this.$toast.fail("发送太过频繁");
        } else if (error.response && error.response.status === 404) {
          return this.$toast.fail("手机号不正确");
        } else {
          return this.$toast.fail("发送失败");
        }
      }
      this.isCountDownShow = true;
    },
  },
};
</script>

<style lang= 'less' scoped >
/deep/ .heima {
  font-size: 40px;
}
</style>